package com.wlhse.service.impl;

import com.wlhse.dao.TaskAndProcessDao;
import com.wlhse.entity.TaskAndProcessPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.TaskAndProcessService;
import com.wlhse.util.DefaultUtil;
import com.wlhse.util.DictCode;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TaskAndProcessServiceImpl implements TaskAndProcessService {

    @Resource
    private TaskAndProcessDao taskAndProcessDao;

    @Resource
    private TreeUtil treeUtil;

    @Resource
    private SortCodeUtil sortCodeUtil;

    @Resource
    private DefaultUtil defaultUtil;

    @Override
    public String getTaskAndProcessTree() {
        return NR.r(treeUtil.getTaskAndProcessTree(taskAndProcessDao.getAll()));
    }

    @Override
    public String getTaskAndProcessList(String code) {
        if (StringUtils.isBlank(code) || code.length() == 0)
            return NR.r(taskAndProcessDao.queryTask());
        return NR.r(taskAndProcessDao.queryProcessByTask(code));
    }

    @Override
    @Transactional
    public String deleteTaskAndProcess(String nodeCode) {
        if (taskAndProcessDao.deleteTaskAndProcess(nodeCode) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String updateTaskAndProcess(TaskAndProcessPojo taskAndProcessPojo) {
        if (taskAndProcessDao.updateTaskAndProcess(taskAndProcessPojo) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertTaskAndProcess(TaskAndProcessPojo taskAndProcessPojo) {
        int i = -1;
        if (StringUtils.isNotBlank(taskAndProcessPojo.getTaskAndProcessName())) {
            String taskCode = taskAndProcessPojo.getTaskAndProcessCode();
            if (taskCode.length() > DictCode.TASK_AND_PROCESS_LEVEL_1) {
                taskCode = taskCode.substring(0, DictCode.TASK_AND_PROCESS_LEVEL_1);
            }
            String newTaskCode;
            String maxTaskCode;
            if (StringUtils.isNotBlank(taskCode)) {
                newTaskCode = taskAndProcessDao.getMaxString(taskCode, DictCode.TASK_AND_PROCESS_LEVEL_1, DictCode.TASK_AND_PROCESS_LEVEL_2);
                if (StringUtils.isNotBlank(newTaskCode)) {
                    maxTaskCode = sortCodeUtil.getMaxCodeString(newTaskCode);
                } else {
                    maxTaskCode = taskCode + "01";
                }
            } else {
                newTaskCode = taskAndProcessDao.getMaxString(taskCode, 0, DictCode.TASK_AND_PROCESS_LEVEL_1);
                if (StringUtils.isNotBlank(newTaskCode)) {
                    maxTaskCode = sortCodeUtil.getMaxCodeString(newTaskCode);
                } else {
                    maxTaskCode = "01";
                }
                defaultUtil.setTaskAndProcess(maxTaskCode);
            }
            taskAndProcessPojo.setTaskAndProcessCode(maxTaskCode);
            i = taskAndProcessDao.insertSingle(taskAndProcessPojo);
        }
        if (i < 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }

}
